package com.publicize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.publicize.commom.commonutils.JwtUtils;
import com.publicize.commom.commonutils.MD5;
import com.publicize.commom.commonutils.Result;
import com.publicize.commom.servicebase.exception.MyException;
import com.publicize.entity.Manage;
import com.publicize.entity.Vo.ManageLogin;
import com.publicize.mapper.ManageMapper;
import com.publicize.service.ManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 管理员登录表 服务实现类
 * </p>
 *
 * @author bs
 * @since 2021-03-02
 */
@Service
public class ManageServiceImpl extends ServiceImpl<ManageMapper, Manage> implements ManageService {
//登录验证
    @Override
    public String login(ManageLogin manageLogin) {
        //判断用户名密码是否存在
        QueryWrapper<Manage> queryWrapper=new QueryWrapper();
        //判断数据库是否有对应 字段名称的数据
        queryWrapper.eq("manage_username",manageLogin.getManageUsername());
        queryWrapper.eq("manage_password",MD5.encrypt(manageLogin.getManagePassword()));
        //根据条件查找数据库
        Manage manage = baseMapper.selectOne(queryWrapper);
        if(manage.getIsBlacklist().equals("1")){
            throw new MyException("该管理员已被添加至黑名单,无法登录",401);
        }
        //管理员不存在返回异常
        if(manage==null) throw new MyException("用户名或密码错误",201);
        if(manage.getIsBlacklist()==1){
            throw new MyException("用户名或密码错误",202);
        }
        //登录成功返回token
        String token = JwtUtils.getJwtToken(manage.getId(), manage.getManageUsername());
        return token;
    }
//添加管理员
    @Override
    public void addManage(Manage manage) {
        QueryWrapper<Manage> wrapper=new QueryWrapper<>();
        //根据用户名查找管理员
        wrapper.eq("manage_username",manage.getManageUsername());
        Manage selectOne = baseMapper.selectOne(wrapper);
        //如果用户名已存在,不添加
        if(selectOne!=null) throw new MyException("用户名已存在",201);
        //添加管理员
        manage.setManagePassword(MD5.encrypt(manage.getManagePassword()));
        baseMapper.insert(manage);
    }
//修改管理员资料
    @Override
    public void updateManage(Manage manage) {
        //判断管理员是否存在
        Manage manageOne = baseMapper.selectById(manage);
        //不存在
        if(manageOne==null) throw new MyException("该管理员不存在",201);
        //提交修改资料
        int flag = baseMapper.updateById(manage);
        if(flag!=1)   throw new MyException("修改失败",201);
    }
//查询管理员并分页
    @Override
    public Result selectManage(Integer page, Integer limit, String condition) {
        QueryWrapper<Manage> queryWrapper=new QueryWrapper<>();
        //根据条件有无查询
       if(!StringUtils.isEmpty(condition)){
           queryWrapper.like("manage_username",condition);
           queryWrapper.or();
           queryWrapper.eq("id",condition);
           queryWrapper.orderByDesc("create_time");
       }
        //根据分页数,数量查询数据
        Page<Manage> pageManage=new Page<Manage>(page,limit);
        //返回分页数据
        IPage<Manage> manageIPage = baseMapper.selectPage(pageManage, queryWrapper);
        //查询数据总数
        long total = manageIPage.getTotal();
        //查询具体记录
        List<Manage> records = manageIPage.getRecords();
        //返回页数
        long current = manageIPage.getCurrent();
        return Result.ok().data("total",total).data("current",current).data("records",records);
    }
   //删除管理员
    @Override
    public void removeManage(String id) {
        baseMapper.deleteById(id);
    }
    //根据id将管理员添加至黑名单
    @Override
    public Integer BlacklistManage(String id) {
        Manage manage = baseMapper.selectById(id);
        if(manage.getIsBlacklist()==1) {
            manage.setIsBlacklist(0);
        }else {
            manage.setIsBlacklist(1);
        }
        int i = baseMapper.updateById(manage);
        if(i!=1) throw new MyException("修改失败",201);
        return manage.getIsBlacklist();
    }
//删除文件
    @Override
    public void deleteAvatar(String fileName) {
        System.out.println(fileName);
        File file=new File(fileName);

        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + fileName + "成功！");
            } else {
                throw new MyException("删除单个文件" + fileName + "失败！",201);
            }
        } else {
            throw new MyException("删除单个文件失败：" + fileName + "不存在！",201);
        }
    }


}
